
Partial Class admin_appFMSA
    Inherits System.Web.UI.Page
    Private sValue As String

    Protected Sub AddButton_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles AddButton.Click

        FMSAScrollingGridView.Visible = False
        AddButton.Visible = False

        SubmitButton.Visible = True
        CancelButton.Visible = True
        Me.FmsaDetailsView.Visible = True
        Me.hfMode.Value = "INSERT"
        Me.ClearForm()
        Me.hfFmsaId.Value = 0

    End Sub

    Protected Sub FmsaDetailsView_ItemInserting(ByVal sender As Object, _
                                             ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) _
                                             Handles FmsaDetailsView.ItemInserting

        Me.FmsaDetailsView.Visible = False

    End Sub

    Protected Sub FmsaDetailsView_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles FmsaDetailsView.ItemUpdating

        Me.FmsaDetailsView.Visible = False

    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles CancelButton.Click

        SubmitButton.Visible = False
        CancelButton.Visible = False
        Me.FmsaDetailsView.Visible = False

        AddButton.Visible = True
        FMSAScrollingGridView.Visible = True
        Me.ErrorSummary.Visible = False
        Me.ClearForm()
        SetStatus("Action has been cancelled.")

    End Sub

    Protected Sub SubmitButton_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles SubmitButton.Click

        Dim fieldName As TextBox = CType(FmsaDetailsView.FindControl("FieldNameTextBox"), TextBox)
        Dim txtSA1 As TextBox = CType(FmsaDetailsView.FindControl("txtSA1"), TextBox)
        Dim txtSA2 As TextBox = CType(FmsaDetailsView.FindControl("txtSA2"), TextBox)
        Dim txtSA3 As TextBox = CType(FmsaDetailsView.FindControl("txtSA3"), TextBox)
        Dim txtCity As TextBox = CType(FmsaDetailsView.FindControl("txtSACity"), TextBox)
        Dim txtState As TextBox = CType(FmsaDetailsView.FindControl("txtSAState"), TextBox)
        Dim txtZip As TextBox = CType(FmsaDetailsView.FindControl("txtSAZip"), TextBox)
        Dim fieldActive As CheckBox = CType(FmsaDetailsView.FindControl("ActiveCheckBox"), CheckBox)
        Dim txtContactFirstName As TextBox = CType(FmsaDetailsView.FindControl("txtContactFirstName"), TextBox)
        Dim txtContactLastName As TextBox = CType(FmsaDetailsView.FindControl("txtContactLastName"), TextBox)
        Dim txtPhoneNumber As TextBox = CType(FmsaDetailsView.FindControl("txtPhoneNumber"), TextBox)
        Dim phoneNumberInput As InputControls.PhoneInputControl = CType(FmsaDetailsView.FindControl("PhoneNumberInputControl"), InputControls.PhoneInputControl)
        Dim faxNumberInput As InputControls.PhoneInputControl = CType(FmsaDetailsView.FindControl("FaxNumberInputControl"), InputControls.PhoneInputControl)

        Dim txtEmailAddress As TextBox = CType(FmsaDetailsView.FindControl("txtEmailAddress"), TextBox)
        Dim ddlCounty As DropDownList = CType(FmsaDetailsView.FindControl("ddlCounty"), DropDownList)
        Dim hasAddress As Boolean = False
        Dim fmsaId As Integer
        Dim phoneNumber As String
        Dim faxNumber As String

        phoneNumber = StripPhoneNumber(phoneNumberInput.Value)
        faxNumber = StripPhoneNumber(faxNumberInput.Value)

        If fieldName.Text = "" Then
            sValue = "You need to enter a value for Agency Name before continuing."
            SetStatus(sValue)
            'Me.ErrorSummary.SetSingleError(sValue)
            'Me.ErrorSummary.Visible = True
            Exit Sub
        End If

        'CHEK IF THE USER ENTERED ANY PART OF AN ADDRESS
        If (txtSA1.Text.Trim().Length > 0 Or txtSA2.Text.Trim().Length > 0 Or txtSA3.Text.Trim().Length > 0 Or _
            txtCity.Text.Trim().Length > 0 Or txtState.Text.Trim().Length > 0 Or txtZip.Text.Trim().Length > 0) Then

            'VALIDATE THE ADDRESS
            If Not Me.ValidateAddress(txtSA1, txtSA2, txtSA3, txtCity, txtState, txtZip) Then
                sValue = "You need to enter a complete address before continuing."
                'Me.ErrorSummary.SetSingleError(sValue)
                'Me.ErrorSummary.Visible = True
                SetStatus(sValue)
                Exit Sub
            Else
                hasAddress = True
            End If

        End If

        fmsaId = CType(Me.hfFmsaId.Value, Integer)
        
        'CHECK IF AN AGENCY BY THIS NAME ALREADY EXISTS
        If Cdm.DataCore.Entities.FMSA.Exists(fmsaId, fieldName.Text) Then
            sValue = "A FMSA by that name already exists. Please enter a different name."
            'Me.ErrorSummary.SetSingleError(sValue)
            'Me.ErrorSummary.Visible = True
            SetStatus(sValue)
            Exit Sub
        End If

        'For i As Integer = 0 To FMSAScrollingGridView.Rows.Count - 1
        '    If fieldName.Text = FMSAScrollingGridView.Rows(i).Cells(0).Text Then
        '        sValue = "Value already exists in the database. Please enter a different value."
        '        Me.ErrorSummary.SetSingleError(sValue)
        '        Me.ErrorSummary.Visible = True
        '        Exit Sub
        '    End If
        'Next

        If phoneNumber = "" Then
            sValue = "Telephone Number needs to be a valid phone number."
            'Me.ErrorSummary.SetSingleError(sValue)
            'Me.ErrorSummary.Visible = True
            SetStatus(sValue)
            Exit Sub
        End If

        'If faxNumber = "" Then
        ' sValue = "Fax Number needs to be a valid phone number."
        'Me.ErrorSummary.SetSingleError(sValue)
        'Me.ErrorSummary.Visible = True
        'SetStatus(sValue)
        'Exit Sub
        'End If

        SubmitButton.Visible = False
        CancelButton.Visible = False
        FmsaDetailsView.Visible = False

        Dim oFMSA As Cdm.DataCore.Entities.FMSA

        If Me.hfMode.Value.ToUpper() = "EDIT" Then
            oFMSA = Cdm.DataCore.Entities.FMSA.Retrieve(fmsaId)
        Else
            oFMSA = New Cdm.DataCore.Entities.FMSA()
        End If

        With oFMSA
            .AgencyName = fieldName.Text.Trim()
            .IsActive = fieldActive.Checked
            .ContactFirstName = txtContactFirstName.Text.Trim()
            .ContactLastName = txtContactLastName.Text.Trim()
            If hasAddress Then
                .Address1 = txtSA1.Text.Trim()
                .Address2 = txtSA2.Text.Trim()
                .Address3 = txtSA3.Text.Trim()
                .City = txtCity.Text.Trim()
                .State = txtState.Text.Trim()
                .Zip = txtZip.Text.Trim()
            End If
            .PhoneNumber = phoneNumber
            .FaxNumber = faxNumber
            .EmailAddress = txtEmailAddress.Text.Trim()
            If ddlCounty.SelectedItem IsNot Nothing Then
                .CountyId = ddlCounty.SelectedValue.ToString().Trim()
            Else
                .CountyId = 0
            End If
            .DateModified = Now()
            .LastModified = UserAuthorization.CurrentUser.LogonId
            .Persist()
            SetStatus("Agency Information Saved.")
        End With

        Me.FmsaObjectDataSource.Select()
        FMSAScrollingGridView.DataBind()

        Me.ClearForm()

        If ErrorSummary.Visible = True Then
            ErrorSummary.Visible = False
        End If
        FMSAScrollingGridView.Visible = True
        AddButton.Visible = True

    End Sub

    Protected Function StripPhoneNumber(ByVal phoneNumberString As String) As String
        Dim counter As Integer
        Dim phoneNumber As String = ""
        For counter = 0 To phoneNumberString.Length - 1
            Try
                Convert.ToInt32(phoneNumberString.Substring(counter, 1))
                phoneNumber = phoneNumber + phoneNumberString.Substring(counter, 1)
            Catch ex As Exception
            End Try
        Next
        If phoneNumber.Length = 10 Then
            Return phoneNumber
        Else
            Return ""
        End If
    End Function

    Protected Function FormatPhoneNumber(ByVal phoneNumberString As String) As String
        Dim phoneNumber As String = ""
        Try
            phoneNumber = "(" + phoneNumberString.Substring(0, 3) + ")" + phoneNumberString.Substring(3, 3) + "-" + phoneNumberString.Substring(6)
        Catch ex As Exception
        End Try
        Return phoneNumber
    End Function

    Private Function ValidateAddress(ByVal SA1 As TextBox, ByVal SA2 As TextBox, ByVal SA3 As TextBox, ByVal City As TextBox, ByVal State As TextBox, ByVal Zip As TextBox) As Boolean

        If (SA1.Text.Trim().Length > 0 And City.Text.Trim().Length > 0 And State.Text.Trim().Length > 0 And Zip.Text.Trim().Length > 0) Then
            Return True
        Else
            Return False
        End If

    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ClearStatus()
        SetupJavascript()
        If Not Page.IsPostBack Then
            Dim fmsaText As String = EntityController.GetSingleDisplayName("FMSA")
            If fmsaText.ToUpper.Contains("AGENCY") Then
                Me.FmsaLabel.Text = fmsaText
            Else
                Me.FmsaLabel.Text = fmsaText & " Agency"
            End If
            Me.FMSAScrollingGridView.Sort("AgencyName", SortDirection.Ascending)
        End If

    End Sub


    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.FMSAScrollingGridView.DivClientId, CInt(Me.FMSAScrollingGridView.ToolTip))
        EntityController.JavaScriptAddChildDiv(Me.DetailsPanel.ClientID, 90)
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.FMSAScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(28)
    End Sub

    Private Sub ClearStatus()
        SetStatus("")
    End Sub
    Private Sub SetStatus(ByVal message As String)
        Dim mymaster As ASP.admin_admin_master = CType(Me.Master, ASP.admin_admin_master)
        mymaster.SetStatus(message)
    End Sub


    Private Sub SetForm(ByVal fmsaId As Integer)

        Dim oFmsa As Cdm.DataCore.Entities.FMSA
        Dim fieldName As TextBox = CType(FmsaDetailsView.FindControl("FieldNameTextBox"), TextBox)
        Dim txtSA1 As TextBox = CType(FmsaDetailsView.FindControl("txtSA1"), TextBox)
        Dim txtSA2 As TextBox = CType(FmsaDetailsView.FindControl("txtSA2"), TextBox)
        Dim txtSA3 As TextBox = CType(FmsaDetailsView.FindControl("txtSA3"), TextBox)
        Dim txtCity As TextBox = CType(FmsaDetailsView.FindControl("txtSACity"), TextBox)
        Dim txtState As TextBox = CType(FmsaDetailsView.FindControl("txtSAState"), TextBox)
        Dim txtZip As TextBox = CType(FmsaDetailsView.FindControl("txtSAZip"), TextBox)
        Dim fieldActive As CheckBox = CType(FmsaDetailsView.FindControl("ActiveCheckBox"), CheckBox)
        Dim txtContactFirstName As TextBox = CType(FmsaDetailsView.FindControl("txtContactFirstName"), TextBox)
        Dim txtContactLastName As TextBox = CType(FmsaDetailsView.FindControl("txtContactLastName"), TextBox)
        Dim phoneNumberInput As InputControls.PhoneInputControl = CType(FmsaDetailsView.FindControl("PhoneNumberInputControl"), InputControls.PhoneInputControl)
        Dim faxNumberInput As InputControls.PhoneInputControl = CType(FmsaDetailsView.FindControl("FaxNumberInputControl"), InputControls.PhoneInputControl)

        Dim txtEmailAddress As TextBox = CType(FmsaDetailsView.FindControl("txtEmailAddress"), TextBox)
        Dim ddlCounty As DropDownList = CType(FmsaDetailsView.FindControl("ddlCounty"), DropDownList)

        oFmsa = Cdm.DataCore.Entities.FMSA.Retrieve(fmsaId)

        With oFmsa
            fieldName.Text = .AgencyName
            txtSA1.Text = .Address1
            txtSA2.Text = .Address2
            txtSA3.Text = .Address3
            txtCity.Text = .City
            txtState.Text = .State
            txtZip.Text = .Zip
            fieldActive.Checked = .IsActive
            txtContactFirstName.Text = .ContactFirstName
            txtContactLastName.Text = .ContactLastName
            phoneNumberInput.Value = .PhoneNumber
            faxNumberInput.Value = .FaxNumber
            txtEmailAddress.Text = .EmailAddress
            If .CountyId <> 0 Then
                ddlCounty.SelectedIndex = ddlCounty.Items.IndexOf(ddlCounty.Items.FindByValue(.CountyId))
            End If
        End With

        Me.hfFmsaId.Value = fmsaId
        Me.hfMode.Value = "EDIT"

    End Sub

    Private Sub ClearForm()

        Dim fieldName As TextBox = CType(FmsaDetailsView.FindControl("FieldNameTextBox"), TextBox)
        Dim txtSA1 As TextBox = CType(FmsaDetailsView.FindControl("txtSA1"), TextBox)
        Dim txtSA2 As TextBox = CType(FmsaDetailsView.FindControl("txtSA2"), TextBox)
        Dim txtSA3 As TextBox = CType(FmsaDetailsView.FindControl("txtSA3"), TextBox)
        Dim txtCity As TextBox = CType(FmsaDetailsView.FindControl("txtSACity"), TextBox)
        Dim txtState As TextBox = CType(FmsaDetailsView.FindControl("txtSAState"), TextBox)
        Dim txtZip As TextBox = CType(FmsaDetailsView.FindControl("txtSAZip"), TextBox)
        Dim fieldActive As CheckBox = CType(FmsaDetailsView.FindControl("ActiveCheckBox"), CheckBox)
        Dim txtContactFirstName As TextBox = CType(FmsaDetailsView.FindControl("txtContactFirstName"), TextBox)
        Dim txtContactLastName As TextBox = CType(FmsaDetailsView.FindControl("txtContactLastName"), TextBox)

        Dim phoneNumberInput As InputControls.PhoneInputControl = CType(FmsaDetailsView.FindControl("PhoneNumberInputControl"), InputControls.PhoneInputControl)
        Dim faxNumberInput As InputControls.PhoneInputControl = CType(FmsaDetailsView.FindControl("FaxNumberInputControl"), InputControls.PhoneInputControl)

        Dim txtEmailAddress As TextBox = CType(FmsaDetailsView.FindControl("txtEmailAddress"), TextBox)
        Dim ddlCounty As DropDownList = CType(FmsaDetailsView.FindControl("ddlCounty"), DropDownList)

        fieldName.Text = String.Empty
        txtSA1.Text = String.Empty
        txtSA2.Text = String.Empty
        txtSA3.Text = String.Empty
        txtCity.Text = String.Empty
        txtState.Text = String.Empty
        txtZip.Text = String.Empty
        txtContactFirstName.Text = String.Empty
        txtContactLastName.Text = String.Empty
        phoneNumberInput.Value = String.Empty
        faxNumberInput.Value = String.Empty
        txtEmailAddress.Text = String.Empty
        fieldActive.Checked = True

    End Sub

    Protected Sub FMSAScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles FMSAScrollingGridView.RowDataBound

        If e.Row.DataItemIndex = 0 Then
            AccessibilityController.AddImageToSortableGridHeaders(FMSAScrollingGridView)
        End If
        Dim AgencyTextBox As TextBox = CType(e.Row.FindControl("AgencyTextBox"), TextBox)
        If Not AgencyTextBox Is Nothing Then
            SetFocus(AgencyTextBox.ClientID)
        End If
        Dim activeCheckBox As CheckBox = e.Row.FindControl("IsActiveCheckBox")
        If Not activeCheckBox Is Nothing Then
            activeCheckBox.InputAttributes.Add("title", "check if Agency " & CType(e.Row.Cells(0).Controls(0), DataBoundLiteralControl).Text & " is active.")
        End If
    End Sub

    Protected Sub FMSAScrollingGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles FMSAScrollingGridView.RowEditing
        Dim fmsaId As Integer = Me.FMSAScrollingGridView.DataKeys(e.NewEditIndex()).Value

        FMSAScrollingGridView.Visible = False
        AddButton.Visible = False

        SubmitButton.Visible = True
        CancelButton.Visible = True
        Me.FmsaDetailsView.Visible = True

        Me.SetForm(fmsaId)
        e.Cancel = True
    End Sub
End Class
